package battlezone;

import java.io.*;
import java.net.*;
import java.util.*;

/**
 * ParameterObject reads from and uses the defaults in the
 * parameter file.  If the parameter file is not found the object
 * defaults to predefined defaults.
 */
public class ParameterObject {
    private static ParameterObject _instance;
    private float projectileVelocity;
    private float tankPivotRate;
    private float tankSpeed;
    private float rateOfFire;
    private short initialNumberOfTanks;
    private InetAddress[] ipAddresses;
    private short simultaneousProjectileCount;
    private float engageabilityRange;
    private float detectabilityRange;
    private short initialAmmunitionCount;
    private File moveSoundFile;
    private File fireSoundFile;
    private File killSoundFile;

    //if an instance of parameterObject exists use it, otherwise
    //create an instance for use.
    public static ParameterObject getInstance() {
        if (_instance == null) {
            _instance = new ParameterObject();
        }
        return _instance;
    }
    
    private ParameterObject() {

        //play multi player game configured from the parameter file
        try {

            //Get the parameterFile from FileManager
            //Open then read the setting from parameter object.
            File parameterFile = FileManager.getInstance().parameterFile;
            FileReader fname = new FileReader(parameterFile);
            BufferedReader bFileName = new BufferedReader(fname);

            // Create an ArrayList to read file names
            ArrayList<String> al = new ArrayList<String>();
            String fName;
            while ((fName = bFileName.readLine()) != null) {
                al.add(fName);
            }

            // Close file input stream
            fname.close();
            bFileName.close();

            // Convert al to an Array to use file content
            Object aNames[] = al.toArray();
            int i;
            i = 0;
            // initial number of tanks per player
            this.initialNumberOfTanks = Short.parseShort(((String) aNames[i]).trim());

            // tank speed
            this.tankSpeed = Float.valueOf(((String) aNames[i + 1]).trim()).floatValue();

            // tank turn rate
            this.tankPivotRate = Float.valueOf(
                    ((String) aNames[i + 2]).trim()).floatValue();

            // projectile velocity
            this.projectileVelocity = Float.valueOf(
                    ((String) aNames[i + 3]).trim()).floatValue();

            // maximum simultaneous projectile count
            this.simultaneousProjectileCount = Short.parseShort(((String) aNames[i + 4]).trim());

            // rate of fire
            this.rateOfFire = Float.valueOf(((String) aNames[i + 5]).trim()).floatValue();

            // initial ammunition count
            this.initialAmmunitionCount = Short.parseShort(((String) aNames[i + 6]).trim());

            // detectability range
            this.detectabilityRange = Float.valueOf(
                    ((String) aNames[i + 7]).trim()).floatValue();

            // engageability range
            this.engageabilityRange = Float.valueOf(
                    ((String) aNames[i + 8]).trim()).floatValue();

            // sound filename for type move
            File moveSoundFile = new File(((String) aNames[i + 9]));
            this.moveSoundFile = moveSoundFile;

            // sound filename for type fire
            File fireSoundFile = new File(((String) aNames[i + 10]));
            this.fireSoundFile = fireSoundFile;

            // sound filename for type kill
            File killSoundFile = new File(((String) aNames[i + 11]));
            this.killSoundFile = killSoundFile;

            //The rest of the file contains ipAddresses.
            //Recall that the file starts on line zero so length of the
            //array - 1 is the file length then subtract off what we've
            //already read.
            int index = 0;
            InetAddress[] localIPAddresses = new InetAddress[(aNames.length - 1) - (i + 11)];
            //InetAddress ipAddresses[];
            for (int z = i + 12; z < aNames.length; z++) {
                localIPAddresses[index] = InetAddress.getByName(aNames[z].toString());
                index = index + 1;
                //ipAddresses. = (InetAddress) aNames[z];
            }
        // IP addresses of participating computers, one per line
        this.ipAddresses = localIPAddresses;

        //else single player game
        } catch (Exception e) {
            //number of tanks
            System.out.append("Error - setting game for single play/no sound effects: ");
            e.printStackTrace();
            
            this.initialNumberOfTanks = 3;

            // tank speed
            this.tankSpeed = 15;

            // tank turn rate
            this.tankPivotRate = 30;

            // projectile velocity
            this.projectileVelocity = 60;

            // maximum simultaneous projectile count
            this.simultaneousProjectileCount = 3;

            // rate of fire
            this.rateOfFire = 120;

            // initial ammunition count
            this.initialAmmunitionCount = 12;

            // detectability range
            this.detectabilityRange = 200;

            // engageability range
            this.engageabilityRange = 150;

            // sound filename for type move
            this.moveSoundFile = null;

            // sound filename for type fire
            this.fireSoundFile = null;

            // sound filename for type kill
            this.killSoundFile = null;
            
            // IP addresses of participating computers, one per line
            this.ipAddresses = new InetAddress[0];
            
            //System.out.append("Exception: ");
            //e.printStackTrace();
        }
    }

    public float getEngageabilityRange() {
        return engageabilityRange;
    }

    public File getFireSoundFile() {
        return fireSoundFile;
    }

    public short getInitialAmmunitionCount() {
        return initialAmmunitionCount;
    }

    public short getInitialNumberOfTanks() {
        return initialNumberOfTanks;
    }

    public InetAddress[] getIpAddresses() {
        return ipAddresses;
    }

    public File getKillSoundFile() {
        return killSoundFile;
    }

    public File getMoveSoundFile() {
        return moveSoundFile;
    }

    public float getProjectileVelocity() {
        return projectileVelocity;
    }

    public short getSimultaneousProjectileCount() {
        return simultaneousProjectileCount;
    }

    public float getTankPivotRate() {
        return tankPivotRate;
    }

    public float getTankSpeed() {
        return tankSpeed;
    }

    public float getRateOfFire() {
        return rateOfFire;
    }

    public float getDetectabilityRange() {
        return detectabilityRange;
    }
}
